import {
  checkBussiness,
  checkCode,
  businessPublish,
  userOrder,
  maybeLike,
  checkOrder,
} from '../../utils/api.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    good_shop: 1,
    gonglv: {
      id: "",
      attack_id: "",
      p: ""
    },
    maybeData: {},
    maybeListData: [],
    currentIndex: 0,
    nickName: "",
    avatarUrl: "",
    isShop: "",
    currentIndexs: 0,
    text: "获取验证码",
    bussinessData: {
      phone: "",
    },
    showModals: false,
    currentTime: 120,
    disabled: false,
    codeData: {
      phone: "",
      code: ""
    },
    orderData: {
      p: 1,
      state: ""
    },
    is_check: false,
  },
  //大标题
  publishList: function (e) {
    wx.navigateTo({
      url: './publishlist/publishlist?data=' + e.currentTarget.dataset.type,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    var that = this
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          avatarUrl: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName
        })
      }
    })
    that.mayBelikes()
    wx.getStorage({
      key: 'is_shop',
      success: function (res) {
        that.setData({
          isShop: res.data
        })
      },
      fail: res => {
        that.setData({
          isShop: 1
        })
      }
    })
    wx.getStorage({
      key: 'shop_type',
      success: function (res) {
        that.setData({
          good_shop: res.data
        })
      }
    });
    wx.getStorage({
      key: 'is_check',
      success: function (res) {
        that.setData({
          is_check: res.data
        })
      }
    });
  },
  // onShow:function(){
  //   this.onLoad()
  // },
  //可能喜欢
  mayBelikes() {
    maybeLike(this.data.maybeData).then(res => {
      if (res.code == 200) {
        this.setData({
          maybeListData: res.datas
        })
      }
    })
  },
  //用户查看订单
  userClick() {
    wx.navigateTo({
      url: "../ordersearch/ordersearch"
    })
  },
  //商家查看
  businessClick() {
    wx.navigateTo({
      url: "../businessOrder/businessOrder"
    })
  },
  cancel() {
    this.setData({
      showModals: false
    })
  },
  hideModal() {
    this.setData({
      showModals: false
    })
  },
  //检查商户
  checkshanghu(e) {
    var that = this
    that.setData({
      showModals: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  phoneBlur(e) {
    let phonevalue = e.detail.value
    let bussinessPhone = 'bussinessData.phone';
    let codeDataPhone = 'codeData.phone';
    this.setData({
      [bussinessPhone]: phonevalue,
      [codeDataPhone]: phonevalue
    })
  },
  //获取验证码点击事件
  bindButtonTap: function () {
    var that = this;
    that.setData({
      disabled: true, //只要点击了按钮就让按钮禁用 （避免正常情况下多次触发定时器事件）
      color: '#ccc',
    })
    var phone = that.data.bussinessData.phone;
    var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值
    var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空
    if (phone == '') {
      warn = "号码不能为空";
    } else if (phone.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(phone)) {
      warn = "手机号格式不正确";
    } else {
      this.checkBusy()
    };
    //判断 当提示错误信息文字不为空 即手机号输入有问题时提示用户错误信息 并且提示完之后一定要让按钮为可用状态 因为点击按钮时设置了只要点击了按钮就让按钮禁用的情况
    if (warn != null) {
      wx.showModal({
        title: '提示',
        content: warn
      })
      that.setData({
        disabled: false,
        color: '#929fff'
      })
      return;
    }
    ;
  },
  formSubmit(e) {
    let codeDatacode = 'codeData.code';
    this.setData({
      [codeDatacode]: e.detail.value.code
    })
    checkCode(this.data.codeData).then(res => {
      if (res.code == 200) {
        this.setData({
          showModals: false
        })
        wx.setStorage({
          key: 'is_shop',
          data: res.datas.is_shop,
        });
        if ([2,3].includes(res.datas.is_shop)) {
          wx.setStorage({
            key: 'is_check',
            data: true,
          });
        }
        if (res.datas.type != 1) {
          wx.setStorage({
            key: 'shop_type',
            data: 1,
          });
          this.setData({
            good_shop: 1
          })
        } else {
          wx.setStorage({
            key: 'shop_type',
            data: 2,
          });
          this.setData({
            good_shop: 2
          })
        }

        this.onShow()
      } else if (res.code == 134) {
        wx.showToast({
          title: "验证码错误"
        })
      }
      //134错误
    })
  },
  wallet() {
    wx.navigateTo({
      url: './wallet/wallet',
    })
  },
  /**
   * 调取相机扫描
   * */
  openCheck(e) {
    wx.scanCode({
      success: (res) => {
        checkOrder({order_qrCode: res.result}).then(res => {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          });
        });
      },
      fail: (res) => {
      }
    });
  },
  //验证商家
  checkBusy() {
    var that = this
    checkBussiness(this.data.bussinessData).then(res => {
      if (res.code == 610) {
        wx.showToast({
          title: res.msg
        })
        that.setData({
          disabled: false, //只要点击了按钮就让按钮禁用 （避免正常情况下多次触发定时器事件）
          color: "#929fff"
        })
      } else {
        //当手机号正确的时候提示用户短信验证码已经发送
        wx.showToast({
          title: '短信验证码已发送',
          icon: 'none',
          duration: 2000
        });
        //
        var interval = setInterval(function () {
          that.data.currentTime--; //每执行一次让倒计时秒数减一
          that.setData({
            text: that.data.currentTime + 's', //按钮文字变成倒计时对应秒数
          })
          //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字
          if (that.data.currentTime <= 0) {
            clearInterval(interval)
            that.setData({
              text: '重新发送',
              currentTime: 120,
              disabled: false,
              color: '#929fff'
            })
          }
        }, 100);
      }
    })
  },
  skipArea(e) {
    let id = 'gonglv.id';
    let attack_id = 'gonglv.attack_id'
    this.setData({
      [id]: e.currentTarget.dataset.id.id,
      [attack_id]: e.currentTarget.dataset.id.attack_id
    })
    let gonglv = JSON.stringify(this.data.gonglv)
    wx.navigateTo({
      url: '../strategy/details/details?data=' + gonglv,
    })
  },
  // 预览图片
  previewIamge(event) {
    const nowIndex = event.currentTarget.dataset.id;
    const imgList = event.currentTarget.dataset.list
    wx.previewImage({
      current: imgList[nowIndex], //当前预览的图片
      urls: imgList, //所有要预览的图片
    })
  },
  pulish(e) {
    wx.navigateTo({
      url: './pulish/pulish',
    })
  },
  verification(e) {
    wx.navigateTo({
      url: './verification/verification',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function() {
  //   var that=this
  //   wx.getStorage({
  //     key: 'is_shop',
  //     success: function (res) {
  //       that.setData({
  //         isShop: res.data
  //       })
  //     },
  //     fail: res => {
  //       that.setData({
  //         isShop: 1
  //       })
  //     }
  //   })
  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // var that = this
    // wx.showToast({
    //   title: '正在刷新数据...',
    //   icon: 'loading',
    //   duration: 2000
    // })
    // that.mayBelikes()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})